package com.kuang.demo.service.wx;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kuang.demo.mapper.wx.WxCategoryMapper;
import com.kuang.demo.pojo.wx.Cskaoyanmall_category;
import com.kuang.demo.vo.QueryCategoryVO;
import com.kuang.demo.vo.wx.WxQueryCategoryIndexVO;
import com.kuang.demo.vo.wx.WxQueryCurrentCategoryVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by 郭浩 on 2020/11/5 17:53
 */
@Service
public class WxCategoryServiceImpl implements WxCategoryService{
    @Autowired
    WxCategoryMapper wxCategoryMapper;
    @Override
    public WxQueryCurrentCategoryVO queryCategoryById(Integer id) {
        Cskaoyanmall_category cskaoyanmall_category = wxCategoryMapper.selectById(id);
        //根据pid查找子品类
        QueryWrapper<Cskaoyanmall_category> cskaoyanmall_categoryQueryWrapper = new QueryWrapper<>();
        cskaoyanmall_categoryQueryWrapper.eq("pid",id);
        List<Cskaoyanmall_category> cskaoyanmall_categories = wxCategoryMapper.selectList(cskaoyanmall_categoryQueryWrapper);

        WxQueryCurrentCategoryVO wxQueryCurrentCategoryVO = new WxQueryCurrentCategoryVO();
        wxQueryCurrentCategoryVO.setCurrentCategory(cskaoyanmall_category);
        wxQueryCurrentCategoryVO.setCurrentSubCategory(cskaoyanmall_categories);
        return wxQueryCurrentCategoryVO;
    }

    @Override
    public WxQueryCategoryIndexVO indexCategory() {
        WxQueryCategoryIndexVO wxQueryCategoryIndexVO = new WxQueryCategoryIndexVO();
        //查找所有pid为0得一级品类
        QueryWrapper<Cskaoyanmall_category> cskaoyanmall_categoryQueryWrapper = new QueryWrapper<>();
        cskaoyanmall_categoryQueryWrapper.eq("pid",0);
        List<Cskaoyanmall_category> cskaoyanmall_categories = wxCategoryMapper.selectList(cskaoyanmall_categoryQueryWrapper);

        //拿到列得第一个，通过id使用queryCategoryByid方法找到wxQueryCategoryIndexVO得另外两个属性
        Cskaoyanmall_category cskaoyanmall_category = cskaoyanmall_categories.get(0);
        WxQueryCurrentCategoryVO wxQueryCurrentCategoryVO = queryCategoryById(cskaoyanmall_category.getId());

        wxQueryCategoryIndexVO.setCurrentCategory(wxQueryCurrentCategoryVO.getCurrentCategory());
        wxQueryCategoryIndexVO.setCategoryList(cskaoyanmall_categories);
        wxQueryCategoryIndexVO.setCurrentSubCategory(wxQueryCurrentCategoryVO.getCurrentSubCategory());

        return wxQueryCategoryIndexVO;
    }
}
